import 'package:base_app/commons/constants/app_constant.dart';
import 'package:base_app/commons/widgets/webview/handler/base_handler.dart';
import 'package:base_app/commons/widgets/webview/handler/contacts_handler.dart';
import 'package:base_app/commons/widgets/webview/handler/device_control_handler.dart';
import 'package:base_app/commons/widgets/webview/handler/device_info_handler.dart';
import 'package:base_app/commons/widgets/webview/handler/device_size_handler.dart';
import 'package:base_app/commons/widgets/webview/handler/file_picker_handler.dart';
import 'package:base_app/commons/widgets/webview/handler/installed_apps_handler.dart';
import 'package:base_app/commons/widgets/webview/handler/package_info_handler.dart';
import 'package:base_app/commons/widgets/webview/handler/tts_handler.dart';
import 'package:base_app/commons/widgets/webview/handler/voice_record_handler.dart';
import 'package:base_app/utils/url_launcher_util.dart';
import 'package:flutter/material.dart';
import 'handler/asset_picker_handler.dart';
import 'handler/toast_handler.dart';
import 'in_app_web_js_handler_manager.dart';

class InAppWebJSBridgeRegister {
  late InAppWebJSHandlerManager _inAppWebJSHandlerManager;
  late BuildContext context;

  InAppWebJSBridgeRegister(
      {required InAppWebJSHandlerManager inAppWebJSHandlerManager}) {
    _inAppWebJSHandlerManager = inAppWebJSHandlerManager;
  }

  void setContext(BuildContext context) {
    this.context = context;
  }

  // 注册handlers
  void registerHandlers({dynamic jsChannelRegisterHandler}) {

    List<BaseHandler> handlers = [
      ToastHandler(),
      InstalledAppsHandler(),
      ContactsHandler(),
      TtsHandler(),
      PackageInfoHandler(),
      DeviceInfoHandler(),
      DeviceControlHandler(),
      AssetPickerHandler(),
      FilePickerHandler(),
      VoiceRecordHandler(),
      DeviceSizeHandler()
    ];

    for (var handler in handlers) {
      _inAppWebJSHandlerManager.registerHandler(handler.name, (data, responseCallback) {
        handler.setContext(context);
        handler.handler(data, responseCallback);
      });
    }

    _inAppWebJSHandlerManager.addJSBridgeHandlers();
  }

  // 处理是否跳转，true可跳转，false不可跳转
  bool shouldOverrideUrlLoading(Uri uri) {
    ///在页面跳转之前调用 isForMainFrame为false,页面不跳转.导致网页内很多链接点击没效果
    String url = uri.toString();
    return webViewNavigationAllowed(url);
  }

  // 处理是否跳转，true可跳转，false不可跳转
  bool webViewNavigationAllowed(String url) {
    String telPrefix = "tel://";
    String smsPrefix = "sms://";
    String appPrefix = "${AppConstant.customerScheme}://";
    if (url.startsWith(telPrefix)) {
      String data = url.substring(telPrefix.length);
      String uri = "tel:$data";
      UrlLauncherUtil.launchURL(uri);
      // 不可跳转
      return false;
    }

    if (url.startsWith(smsPrefix)) {
      String data = url.substring(smsPrefix.length);

      // 不可跳转
      return false;
    }

    if (url.startsWith(appPrefix)) {
      // app://close
      return false;
    }

    if (url == "about:blank") {
      // 空页面进行跳转
      return true;
    }

    // 可跳转
    return true;
  }
}
